package lib.self.ex.activity;

import android.graphics.drawable.ClipDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.view.Gravity;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import lib.self.R;

/**
 * description: WebViewActivityEx
 * author: Steven Xiao
 * date: 2017/5/8
 */
public abstract class WebViewActivityEx extends ActivityEx {

    private WebView mWebView;
    private ProgressBar mProgressBar;

    private WebSettings mSettings;

    @Override
    public int getContentViewId() {
        return R.layout.activity_webview_ex;
    }

    @Override
    public void findViews() {
        mWebView = findViewById(R.id.webView_ex_wv);
        mProgressBar = findViewById(R.id.webView_ex_progressBar);
    }

    @Override
    public void setViewsValue() {
        Drawable drawable = getProgressBarDrawable();
        if (drawable != null) {
            ClipDrawable clipDrawable = new ClipDrawable(drawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);
            mProgressBar.setProgressDrawable(clipDrawable);
        }

        /**
         *设置WebView的属性
         */
        mSettings = mWebView.getSettings();
        mSettings.setCacheMode(getCacheMode());
        mSettings.setJavaScriptEnabled(enableJs());
        mSettings.setDomStorageEnabled(enableDomStorage());
        mSettings.setAllowFileAccess(true);

//        mSettings.setSavePassword(false);

        //支持手势放大缩小
        mSettings.setUseWideViewPort(enableScale());
        mSettings.setSupportZoom(true);
        mSettings.setBuiltInZoomControls(enableBuiltInZoomControls());
        mSettings.setDisplayZoomControls(false);
        mSettings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
        mSettings.setLoadWithOverviewMode(true);

        mWebView.setScrollBarStyle(getScrollBarStyle());
        mWebView.setWebViewClient(getWebViewClient());

        mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    goneView(mProgressBar);
                    finishLoadingWeb();
                } else {
                    showView(mProgressBar);
                    mProgressBar.setProgress(newProgress);
                }
                super.onProgressChanged(view, newProgress);
            }

            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                setH5Title(title);
            }
        });
    }

    /**
     * 加载界面完成
     */
    protected void finishLoadingWeb(){
    }

    /**
     * 获取网页标题
     *
     * @param title
     */
    public void setH5Title(String title) {

    }

    /**
     * 获取使用的WebViewClient, 方便进行一些加载监听
     *
     * @return
     */
    protected WebViewClient getWebViewClient() {
        return new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (VERSION.SDK_INT < VERSION_CODES.LOLLIPOP) {
                    view.loadUrl(url);
                    return true;
                } else {
                    return super.shouldOverrideUrlLoading(view, url);
                }
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
                    view.loadUrl(request.getUrl().toString());
                    return true;
                } else {
                    return super.shouldOverrideUrlLoading(view, request);
                }
            }
        }

                ;
    }

    /**
     * 是否允许运行JS
     *
     * @return
     */

    protected boolean enableJs() {
        return true;
    }

    /**
     * 允许缩放
     *
     * @return
     * @see #enableBuiltInZoomControls()
     */
    protected boolean enableScale() {
        return true;
    }

    /**
     * 使用放大缩小按钮, 应该和{@link #enableScale()}保持一致
     *
     * @return
     */
    protected boolean enableBuiltInZoomControls() {
        return true;
    }

    /**
     * 是否使用DOM storage API
     *
     * @return
     */
    protected boolean enableDomStorage() {
        return true;
    }

    /**
     * 滚动条风格，默认是不给滚动条留空间，滚动条覆盖在网页上
     *
     * @return
     */
    protected int getScrollBarStyle() {
        return WebView.SCROLLBARS_INSIDE_OVERLAY;
    }

    /**
     * 获取缓存模式
     * <pre>
     * 默认(有网络加载网络, 无网络加载缓存):{@link WebSettings#LOAD_DEFAULT}
     * 无缓存:{@link WebSettings#LOAD_NO_CACHE}
     * 只加载缓存:{@link WebSettings#LOAD_CACHE_ONLY}
     * 加载缓存(无论是否过期), 如果没有则联网:{@link WebSettings#LOAD_CACHE_ELSE_NETWORK}
     *
     * @return 当前模式
     */
    protected int getCacheMode() {
        return WebSettings.LOAD_DEFAULT;
    }

    /**
     * 加载网页
     *
     * @param url
     */
    protected void loadUrl(String url) {
        mWebView.loadUrl(url);
    }


    /**
     * 是否可以前进
     *
     * @return
     */
    protected boolean canGoForward() {
        return mWebView.canGoForward();
    }

    /**
     * 是否可以后退
     *
     * @return
     */
    protected boolean canGoBack() {
        return mWebView.canGoBack();
    }

    /**
     * 前进
     */
    protected void goForward() {
        mWebView.goForward();
    }

    /**
     * 后退
     */
    protected void goBack() {
        mWebView.goBack();
    }

    /**
     * @return 进度条的背景
     */
    protected Drawable getProgressBarDrawable() {
        return null;
    }

    protected WebView getWebView() {
        return mWebView;
    }

    protected WebSettings getSettings() {
        return mSettings;
    }

    /**
     * 解决页面退出 WebView 继续播放视频音乐的问题
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (getWebView() != null) {
            getWebView().destroy();
        }
        ViewGroup view = (ViewGroup) getWindow().getDecorView();
        view.removeAllViews();
    }


    @Override
    protected void onResume() {
        super.onResume();
        if (getWebView() != null) {
            getWebView().onResume();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (getWebView() != null) {
            getWebView().onPause();
        }
    }

}


